package group2.controller;

import group2.model.Admin;
import group2.service.AdminService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @author 周祖航
 * @date 2020/8/17 001714:09
 **/
@Controller
@RequestMapping("/admin")
public class AdminController {

    static Logger logger = Logger.getLogger(AdminController.class);

    @Autowired
    AdminService adminService;

    /*管理系统登录*/
    @RequestMapping("/login")
    public String login(Admin admin, ModelMap modelMap, HttpSession session) {

        Admin logAdmin = this.adminService.adminForLogin(admin);
        // 登录成功, 保存到 HttpSession
        if (logAdmin != null) {
            session.setAttribute("logAdmin", logAdmin);
        } else {
            //  登录失败
            return "redirect:/login.jsp";
        }
        return "redirect:/views/index.jsp";  // 重定向到HouseController.search()
    }
    /*
    * 添加管理员
    * */
    @RequestMapping("/addAdmin")
    public String addAdmin(Admin admin){
        this.adminService.adminForAdd(admin);
        return "redirect:/views/success.jsp";
    }
    /*
    * 显示所有管理员
    * */
    @RequestMapping("/selectAll")
    public String search(ModelMap modelMap,HttpSession session){

        List<Admin> adminList = this.adminService.adminForSelectAll();

        logger.debug(adminList.toString());

        session.setAttribute("adminList",adminList);

        return "redirect:/views/admin-list.jsp";
    }
    //删除管理员
    @RequestMapping("/delete")
    public String delectbyId(int Id){
        adminService.adminForDelete(Id);
        return "redirect:/admin/selectAll";
    }

    //修改管理员
    @RequestMapping("/update")
    public String updateAdmin(Admin admin){
        this.adminService.adminForUpdate(admin);
        return "redirect:/views/success.jsp";
    }
}
